/**
 * Created by joey on 2017/6/27.
 */
import {message} from 'antd';
import {browserHistory} from 'dva/router';

const error = {
  success: {
    code: '200',
    message: '请求成功',
  },
};

export const isSuccess = (...rest) => {
  return rest.every(res => {
    if (!res) return false;
    const tem = res.code == error.success.code;
    if (!tem) {
      if (location.pathname === '/newAlarmList' && res.code === '3100011') return tem;
      if (location.pathname === '/deviceFaultList' && res.code === '3100010') return tem;
      errorMessage(res);
    }
    return tem;
  });
};

export function errorMessage(res) {
  const ignoreCode = ['401'];
  if (ignoreCode.includes(res.code)) return;
  if (res.code === '505') {
    browserHistory.push('/login');
    return;
  }
  if (res.message === 'Network Error') res.message = '网络异常，请检查网络后再试';
  message.error(res.message);
}

export default error;
